package com.sz.jinzheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sz.jinzheng.mapper.UserInfoMapper;
import com.sz.jinzheng.model.PageBean;
import com.sz.jinzheng.model.Role;
import com.sz.jinzheng.model.User;
import com.sz.jinzheng.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public PageBean<User> getUserAll(int index, int size) {
        //利用mybatis分页插件来进行分页数据库
        PageHelper.startPage(index, size);
        List<User> results = userInfoMapper.getUserAll();
        Page<User> page = (Page<User>) results;
        PageBean<User> pageBean = new PageBean<>(page.getTotal(),
                page.getPageSize(), page.getPageNum(), page.getPages(), page.getResult());
        return pageBean;
    }

    /**
     * 查询出对应用户的角色
     *
     * @param uid
     * @return
     */
    @Override
    public List<Role> getUserRoleAll(int uid) {
        return userInfoMapper.getUserRoleAll(uid);
    }

    @Override
    public int addUser(User user) {
        return userInfoMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userInfoMapper.updateUser(user);
    }

    @Override
    public User getUserByKey(Integer uid) {
        return userInfoMapper.getUserByKey(uid);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userInfoMapper.getUserByPhone(phone);
    }
}
